#include"RemoteClient.h"



RemoteClient::RemoteClient(int flag):HClientBase(flag)
{
    qRegisterMetaType<std::string>("string");
    sendFraCli = new SendFrameClient(UDP | IPV4);
    recFraSrv = new RecFrameServer(UDP | IPV4);
    pools = new HzThreadPool(10);
}

RemoteClient::~RemoteClient()
{
    delete pools;
    delete recFraSrv;
    delete sendFraCli;
}

int RemoteClient::startClient(shared_ptr<ServerMsg::LoginMsg> LoginMsgPtr)
{
    //1 连接服务器
    if(sock->Hconnect(serverAdd4) == -1){
        cout<<"连接服务器失败！"<<endl;
        return -1;
    }
    cout<<"连接服务器成功！"<<endl;
    pools->Invoke();
    //2 发送登陆请求
    ServerMsg::MainMsg mainMsg;
    string encoder;
     LoginMsgPtr->SerializeToString(&encoder);
     {
         mainMsg.set_cmd(3);
         mainMsg.set_payload(encoder);
     }
     mainMsg.SerializeToString(&encoder);
     sendData(encoder,sock->s);
    while(1){
        cout<<"wait server msg...."<<endl;
        // 1 等待服务器
        recvData(encoder,sock->s);
        // 2  解析主信息
        mainMsg.ParseFromString(encoder);
        switch (mainMsg.cmd()) {
        case 1://请求开启远程响应
            while(pools->DispatchWorker(new ThreadWorker(&RemoteClient::headRemoteCtl,this,new ServerMsg::MainMsg(mainMsg))) == NULL){
                continue;
            }
            break;
        case 2://当前设备开启远程
            while(pools->DispatchWorker(new ThreadWorker(&RemoteClient::headOpenRemote,this,new ServerMsg::MainMsg(mainMsg))) == NULL){
                continue;
            }
            break;
        case 3://请求登陆响应
            while(pools->DispatchWorker(new ThreadWorker(&RemoteClient::headLogSucess, this,new ServerMsg::MainMsg(mainMsg))) == NULL){
                continue;
            }
            break;
        case 4://请求关闭远程
            while(pools->DispatchWorker(new ThreadWorker(&RemoteClient::headCloseRemote, this,nullptr)) == NULL){
                continue;
            }
            break;
        default:
            break;
        }
    }
    return 0;
}

int RemoteClient::headLogSucess(void* arg)
{
    ServerMsg::MainMsg mainMsg = *(ServerMsg::MainMsg*)arg;
    ServerMsg::LoginMsg loginMsg;
    string encoder;
    cout<<"登陆成功!"<<endl;
    // 解析
    loginMsg.ParseFromString(mainMsg.payload());
    cout<<"code:"<<loginMsg.code()<<endl;
    //通知登陆成功
    this->noteLogSucess(loginMsg.code());
    delete (ServerMsg::MainMsg*) arg;
}

int RemoteClient::headRemoteCtl(void *arg)
{
    cout<<"headRemoteCtl"<<endl;
    ServerMsg::MainMsg* mainMsg = (ServerMsg::MainMsg*)arg;
    if(mainMsg->payload() == "0"){//失败
        this->noteRemoteCtlStatus(0);
    }else{//成功
        //1 新开一个线程开启UDP服务器接收屏幕截图
        std::thread t1(&RemoteClient::openRevFrameSvr,this);
        t1.detach();
        //2 通知
        this->noteRemoteCtlStatus(1);
    }
}

int RemoteClient::reqRemoteCtl(string remoteCode)
{
    //1  封装并发送请求
    ServerMsg::MainMsg msg;
    ServerMsg::RemoteCtlMsg rcmsg;
    string rcString,mainString;
    rcmsg.set_codes(remoteCode);
    rcmsg.set_port(8887);
    rcmsg.set_ipv4(ClientUtils::getLocalHost2Uint32());
    rcmsg.SerializeToString(&rcString);
    msg.set_cmd(1);
    msg.set_payload(rcString);
    msg.SerializeToString(&mainString);
    sendData(mainString,sock->s); 
    return 0;
}

int RemoteClient::reqCloseRemoteCtl()
{
    //1. 封装并发送
    ServerMsg::MainMsg msg;
    msg.set_cmd(4);
    string mainStr;
    msg.SerializeToString(&mainStr);
    sendData(mainStr,sock->s);
}

int RemoteClient::openRevFrameSvr()
{
    cout<<"openRevFrameSvr"<<endl;
    recFraSrv->initServer(4444);
    recFraSrv->startServer();
    return 0;
}

int RemoteClient::headOpenRemote(void* arg)
{
    ServerMsg::MainMsg mainMsg = *(ServerMsg::MainMsg*)arg;
    ServerMsg::RemoteCtlMsg remoteCtlMsg;
    remoteCtlMsg.ParseFromString(mainMsg.payload());
    std::thread t1(&RemoteClient::openSendFrameCli,this,ClientUtils::Uint322Host(remoteCtlMsg.ipv4()));
    t1.detach();
    delete (ServerMsg::MainMsg*) arg;
}

int RemoteClient::headCloseRemote(void *arg)
{
    cout<<"关闭远程控制..."<<endl;
    //关闭data收集、data编码、发送码流客户端
    sendFraCli->datacollect->stop();  //data编码会自动关闭   data编码关闭后  发送端也就自动关闭了
}

int RemoteClient::openSendFrameCli(string ip)
{
    cout<<"openSendFrameCli,target ip:"<<ip<<endl;
    //1.初始化
    sendFraCli->initClient(ip.c_str(),4444);
    //2.开启客户端
    sendFraCli->startClient();
}


